#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

// volatile int flag = 0;

// void handler(int signo){
//     flag = 1;
//     cout << "catch a signo: " << signo << endl;
// }

// int main(){
//     signal(2, handler);

//     while(!flag)
//         ;
//     cout << "process quit!" << endl;

//     return 0;
// }


void handler(int signo){
    cout << "catch a signo: " << signo << endl;

    pid_t rid = waitpid(-1, nullptr, 0);
    if(rid>0){
        cout << "wait child success!" << endl;
    }
}

int main(){
    signal(17, handler);

    pid_t id = fork();
    if(id==0){
        // child
        int cnt = 3;
        while(cnt>0){
            cout << "I am a child process, pid: " << getpid() << endl;
            sleep(1);
            cnt--;
        }
        cout << "child process quit!" << endl;
        exit(0);
    }
    // father
    int cnt = 5;
    while(cnt>0){
        cout << "I am a father process, pid: " << getpid() << endl;
        sleep(1);
        cnt--;
    }

    return 0;
}